package com.project.webservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.webservice.entity.TUserFunc;
import com.project.webservice.entity.TUserXl;
import com.project.webservice.mapper.TUserXlMapper;
import com.project.webservice.service.TUserXlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class TUserXlServiceImpl extends ServiceImpl<TUserXlMapper, TUserXl> implements TUserXlService {

    @DS("rfid")
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateXL(String uid,String adds,String dels)throws Exception{
        log.info("updateXL uid:{},adds:{},dels:{}",uid,adds,dels);
        //删除路线权限
        super.remove(Wrappers.<TUserXl>lambdaQuery().eq(TUserXl::getUid, uid));
        //新增路线权限
        if(StringUtils.isNotBlank(adds)){
            String[] addsArr = adds.split(",");
            List<TUserXl> tUserXls = new ArrayList<>();
            for (String add:addsArr){
                TUserXl tUserXl = new TUserXl();
                tUserXl.setUid(uid);
                tUserXl.setJgxlid(Integer.parseInt(add));
                tUserXls.add(tUserXl);
            }
            super.saveBatch(tUserXls);


            //删除取消的功能权限
            if(StringUtils.isNotBlank(dels)) {
                String[] delsArr = dels.split(",");
                super.remove(Wrappers.<TUserXl>lambdaQuery().eq(TUserXl::getUid, uid)
                        .in(TUserXl::getJgxlid, Arrays.asList(delsArr)));
            }
        }
    }
}
